#ifndef CONFIG_H
#define CONFIG_H

#include <QSettings>
#include <QDebug>
#include <QFile>

class Config {
public:
    inline static Config &instance() {
        static Config instance;
        return instance;
    }

    inline int gameId() const {
        return m_settings->value("game/id").toInt();
    }

    inline QString gameVersion() const {
        return m_settings->value("game/version").toString();
    }

    inline QString updateUrl() const {
        return m_settings->value("update/update-url").toString();
    }

    inline QString popoverUrl() const {
        return m_settings->value("web/popover-url").toString();
    }

    inline QString gameUiUrl() const {
        return m_settings->value("web/game-ui-url").toString();
    }

    inline QString leaderboardUrl() const {
        return m_settings->value("web/leaderboard-url").toString();
    }

    inline QString listenerUrl() const {
        return m_settings->value("danmu/listener-url").toString();
    }

    inline QString upDanmuUrl() const {
        return m_settings->value("danmu/up-danmu-url").toString();
    }

    inline QString customAvatarUrl() const {
        return m_settings->value("danmu/custom-avatar-url").toString();
    }

    inline int autoUploadInterval() const {
        return m_settings->value("danmu/auto-upload-interval").toInt();
    }

    inline QString host() const {
        return m_settings->value("database/host").toString();
    }

    inline int port() const {
        return m_settings->value("database/port").toInt();
    }

    inline QString dbName() const {
        return m_settings->value("database/db-name").toString();
    }

    inline QString dbUserName() const {
        return m_settings->value("database/db-user-name").toString();
    }

    inline QString dbPassword() const {
        return m_settings->value("database/db-password").toString();
    }

private:
    // 初始化配置文件地址
    inline Config(): m_configUrl(":/config.ini")
    {
        loadConfig();
    }

    // 使用配置文件地址加载
    bool loadConfig() {
        if (!QFile(m_configUrl).exists()) {
            qDebug() << "配置文件不存在" << m_configUrl;
            return false;
        }

        qDebug() << "配置文件加载成功" << m_configUrl;
        m_settings = new QSettings(m_configUrl, QSettings::IniFormat);
        return true;
    }

    inline ~Config() { delete m_settings; }
    Config(const Config&) = delete;             // 禁止拷贝
    Config& operator=(const Config&) = delete;  // 禁止赋值

    QSettings *m_settings;
    QString m_configUrl;
};

#endif // CONFIG_H
